﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace update_tool
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
            if(args.Contains("-update"))
            {
                if (args.Count() == 5)
                {
                    string old_file_location = args[2];
                    string new_file_location = args[3];
                    string Proc_name = args[4];
                    string[] str_Array = old_file_location.Split('\\');

                    Console.WriteLine("Args recieved: {0} {1} {2} {3}", args[0], args[1], args[2], args[3]);

                    if (IsProcessOpen(Proc_name))
                    {
                        Console.WriteLine("Found and Killing: {0}", Proc_name);
                        System.Threading.Thread.Sleep(1500);
                        GetProcessOpen(Proc_name).Kill();
                        Console.WriteLine("Killed now wait for close: {0}", Proc_name);
                        GetProcessOpen(Proc_name).WaitForExit(30000);
                        Console.WriteLine("Done: {0}", Proc_name);
                    }
                    else
                    {
                        Console.WriteLine("did not find process: {0}", str_Array.Last());
                    }
                    if (args[1] == "-copy")
                    {
                        Console.WriteLine("copying: {0} to {1}", new_file_location, old_file_location);
                        File.Copy(new_file_location, old_file_location, true);
                        Process.Start(old_file_location);
                    }
                    else if (args[1] == "-move")
                    {
                        Console.WriteLine("move: {0} to {1}", new_file_location, old_file_location);
                        if (File.Exists(old_file_location))
                        {
                            File.Delete(old_file_location);
                        }
                        File.Move(new_file_location, old_file_location);
                        Process.Start(old_file_location);
                    }
                    else
                    {
                        Console.WriteLine("Copy? or move?: {0}", args[1]);
                    }
                }
                else
                {
                    Console.WriteLine("Cound Didnt equal 4: {0}", args.Count());
                    Console.WriteLine("Args recieved: {0} {1} {2} {3}", args[0], args[1], args[2], args[3]);
                }
            }
            }
            catch(IOException e)
            {

            }
//            System.Threading.Thread.Sleep(15000);
        }
        public static bool IsProcessOpen(string name)
        {
            //here we're going to get a list of all running processes on
            //the computer
            foreach (Process clsProcess in Process.GetProcesses())
            {
                //now we're going to see if any of the running processes
                //match the currently running processes. Be sure to not
                //add the .exe to the name you provide, i.e: NOTEPAD,
                //not NOTEPAD.EXE or false is always returned even if
                //notepad is running.
                //Remember, if you have the process running more than once,
                //say IE open 4 times the loop thr way it is now will close all 4,
                //if you want it to just close the first one it finds
                //then add a return; after the Kill
                if (clsProcess.ProcessName.Contains(name))
                {
                    //if the process is found to be running then we
                    //return a true
                    return true;
                }
            }
            //otherwise we return a false
            return false;
        }

        public static Process GetProcessOpen(string name)
        {
            //here we're going to get a list of all running processes on
            //the computer
            foreach (Process clsProcess in Process.GetProcesses())
            {
                //now we're going to see if any of the running processes
                //match the currently running processes. Be sure to not
                //add the .exe to the name you provide, i.e: NOTEPAD,
                //not NOTEPAD.EXE or false is always returned even if
                //notepad is running.
                //Remember, if you have the process running more than once,
                //say IE open 4 times the loop thr way it is now will close all 4,
                //if you want it to just close the first one it finds
                //then add a return; after the Kill
                if (clsProcess.ProcessName.Contains(name))
                {
                    Process theone = clsProcess;
                    //if the process is found to be running then we
                    //return a true
                    return theone;
                }
            }
            //otherwise we return a false
            return null;
        }

    }
}
